#pragma once

#include "RBTree.h"
using my_rb_tree::RBTree;

namespace my_set
{
	template<class K, class Compare = less<K>>
	class set
	{
		typedef K Key;
		typedef K Value;

		struct KeyofValue //方便比较map和set复用代码比较key
		{
			const K& operator()(const Value& data)
			{
				return data;
			}
		};

	public: //set的迭代器都是const类型 防止key被篡改
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_iterator iterator; //使用typename强调此是一个类型
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_iterator const_iterator;
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_reverse_iterator reverse_iterator;
		typedef typename RBTree<Key, Value, Compare, KeyofValue>::const_reverse_iterator const_reverse_iterator;

		iterator begin() {return _t.begin();}
		iterator end() { return _t.end(); }

		const_iterator begin() const { return _t.begin(); }
		const_iterator end() const { return _t.end(); }

		const_iterator cbegin() const { return _t.cbegin(); }
		const_iterator cend() const { return _t.cend(); }

		reverse_iterator rbegin(){ return _t.rbegin(); }
		reverse_iterator rend() { return _t.rend(); }

		const_reverse_iterator rbegin() const { return _t.rbegin(); }
		const_reverse_iterator rend() const { return _t.rend(); }

		const_reverse_iterator crbegin() const { return _t.crbegin(); }
		const_reverse_iterator crend() const { return _t.crend(); }

		iterator insert(const Value& data)
		{
			return (_t.Insert(data)).first;
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		void swap(set<K,Compare>& m)
		{
			_t.swap(m._t);
		}

		set<K,Compare>& operator=(const set<K,Compare>& m)
		{
			_t = m._t;
			return *this;
		}

	private:
		RBTree<Key, Value, Compare, KeyofValue> _t;
	};
}